﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using Yitter.IdGenerator;

namespace CoreShop.App
{
    public partial class frmClassOn : UIForm
    {
        public long orderId = 0;
        private List<corecmsstoremarket> marketList = null;

        public frmClassOn()
        {
            InitializeComponent();
            txtPersons.TextBox.GotFocus += txtSendKey_GotFocus;
        }

        private void txtSendKey_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 2);
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            //marketList = new corecmsstoremarketServices().AsQueryable().Where(p => p.storeId == AppConstVars.sysUser.storeId).ToList();
            marketList = new corecmsstoremarketServices().AsQueryable().ToList();
            mbtMarkets.Clear();
            mbtMarkets.AutoWidth = false;
            mbtMarkets.ItemSize = new System.Drawing.Size(240, 60);
            mbtMarkets.FontSize = 16;
            DateTime thisDay = DateTime.Now;
            foreach (var item in marketList)
            {
                item.starTimeValue = CommFunc.ConvertTimeToDateTime(item.starTime, 0);
                item.endTimeValue = CommFunc.ConvertTimeToDateTime(item.endTime, 0);
                if (item.endTimeValue < item.starTimeValue)
                    item.endTimeValue = CommFunc.ConvertTimeToDateTime(item.endTime, 1);
            }
            var selectItem = marketList.Find(p => p.starTimeValue <= thisDay && p.endTimeValue >= thisDay);
            if (selectItem == null) selectItem = marketList.First();
            foreach (var item in marketList)
            {
                if ((selectItem != null) && (item.id == selectItem.id))
                    mbtMarkets.Add(item.name, item.id, true);
                else
                    mbtMarkets.Add(item.name, item.id, false);
            }
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                double fInput;
                if (!double.TryParse(txtPersons.Text, out fInput))
                {
                    UIMessageBox.Show("请输入有效的数字。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                numPad_OKButtonClick(null, null, 0, 0);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {

        }

        private void txtSendKey_Validating(object sender, CancelEventArgs e)
        {
            double fInput;
            if (!double.TryParse(txtPersons.Text, out fInput))
            {
                MessageBox.Show("请输入有效的数字！");
                e.Cancel = true;
            }
        }

        private void frmInputTxt_Shown(object sender, EventArgs e)
        {
            txtPersons.Focus();
        }


        private void mbtGetRemark_SelectedChanged(object sender, EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            UIButton btn = button.SelectedBtn;
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            var foodorderclassSvr = new corecmsfoodorderclassServices();
            var haveAS = foodorderclassSvr.IsAny(p => p.sysUserId == AppConstVars.sysUser.id && p.classOff == false);
            if (haveAS)
            {
                UIMessageBox.Show("该用户未交班，不能当班。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            decimal money;
            string sInput = txtPersons.Text;
            if (string.IsNullOrEmpty(sInput)) sInput = "0";
            if (!decimal.TryParse(sInput, out money))
            {
                UIMessageBox.Show("请输入合法的钱箱金额。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            long[] taskIds = mbtMarkets.SelectedButtonsValues;
            if (taskIds.Length != 1)
            {
                UIMessageBox.Show("请选择一个班次。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }

            var marketId = taskIds[0];
            var taskSels = marketList.Where(p => p.id == marketId).ToList();
            if (taskSels.Count != 1)
            {
                UIMessageBox.Show("请选择一个班次。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }

            var orderclass = new corecmsfoodorderclass();
            orderclass.id = CommFunc.GetCorpNextId(AppConstVars.sysUser.corpId, YitIdHelper.NextId());
            orderclass.classNumber = ProcessUtil.GetNumberClass(orderclass.id);
            orderclass.sysUserId = AppConstVars.sysUser.id;
            orderclass.marketId = CommFunc.ConvertToInt32(marketId);
            orderclass.starDate = DateTime.Now;
            orderclass.endDate = null;
            orderclass.createTime = DateTime.Now;
            orderclass.classOff = false;
            orderclass.isUpload = false;
            orderclass.cashBoxMoney = money;
            orderclass.storeId = AppConstVars.sysUser.storeId;
            var bl = foodorderclassSvr.InsertAsync(orderclass);
            if (!bl)
            {
                UIMessageBox.Show("提交数据出错。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            AppConstVars.classId = orderclass.id;
            AppConstVars.classNumber = orderclass.classNumber;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtPersons_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                string str = txtPersons.Text;
                int idx = txtPersons.SelectionStart;
                str = str.Insert(idx, ".");
                txtPersons.Text = str;
                txtPersons.SelectionStart = idx + 1;
                txtPersons.Focus();
                e.Handled = true;
            }
        }
    }
}
